
//导入axios
import axios, { isCancel, AxiosError } from 'axios';

console.log('request.js---')
console.log(process.env.NODE_ENV)

axios.defaults.baseURL = 'http://www.jz-energy.cn:3335';
if (process.env.NODE_ENV == 'production') {
  axios.defaults.baseURL = 'http://localhost:19000';
} else if (process.env.NODE_ENV == 'sit') {
  axios.defaults.baseURL = 'http://10.73.113.246:19000';//内网-穿透-安吉5MWh master-机



} else {
  axios.defaults.baseURL = 'http://192.168.50.201:19000';
  axios.defaults.baseURL = 'http://10.73.113.246:19000';  //内网-穿透-安吉5MWh master-机

}


if (process.env.NODE_ENV == 'development') {
  axios.defaults.baseURL = 'http://10.73.113.246:19000';  //内网-穿透-安吉5MWh master-机
} else {
  axios.defaults.baseURL = "http://" + window.location.href.split(":")[1].split("//")[1] + ":19000"
}



let base_url = axios.defaults.baseURL;



export function axios_get(url, params) {


  axios.defaults.headers.token = window.sessionStorage.getItem('token' || 123456);
  axios.defaults.headers.langs = window.sessionStorage.getItem('langs' || 'zh');

  // console.log("base_url", base_url, "url", url)
  return new Promise((resolve, reject) => {
    axios.get(url, {
      params: params
    }).then(function (response) {
      resolve(response.data) //status==200


      if (response.data.code == 401) {  //token过期了。

        window.sessionStorage.setItem("401", "过期token");

        setTimeout(() => {
          window.location.href = "/login";
        }, 100)

      }

      // console.log(response);
    }).catch(errs => {

    })
  });
}


export function axios_post(url, params, res_type) {

  if (res_type == undefined) {
    res_type = 'json';

  }

  // let isChrome = navigator.userAgent.indexOf('Chrome') > -1 && navigator.userAgent.indexOf('Edg') < 0;

  // if (isChrome) {

  // } else {
  //   setTimeout(() => {
  //     window.location.href = "/login?chrome=0";

  //   }, 100)
  // }

  axios.defaults.headers.token = window.sessionStorage.getItem('token' || 123456);
  axios.defaults.headers.langs = window.sessionStorage.getItem('langs' || 'zh');

  // if(res_type){
  //   axios.defaults.headers.
  // }

  console.log('%c %s %c %s', 'color:blue;background: red;', base_url, 'color: white;background-color: green;', url,);


  return new Promise((resolve, reject) => {
    axios.post(url, params, { responseType: res_type }).then(function (response) {
      // console.log(response, response.data.code);
      resolve(response.data) //status==200

      if (response.data.code == 401) {  //token过期了。

        window.sessionStorage.setItem("401", "过期token");

        setTimeout(() => {
          window.location.href = "/login";
        }, 100)

      }
    }).catch(errs => {
    })
  });
}

export function req_action(url, method, data) {

  axios.defaults.headers.token = window.sessionStorage.getItem('token' || 123456);
  axios.defaults.headers.langs = window.sessionStorage.getItem('langs' || 'zh');

  //使用axios下面的create([config])方法创建axios实例，其中config参数为axios最基本的配置信息。
  return new Promise((resolve, reject) => {

    axios.create({
      baseUrl: base_url, //请求后端数据的基本地址，自定义
      timeout: 10000,                  //请求超时设置，单位ms
      url: url,
      method: method || 'get', // default
      headers: {
        'X-Requested-With': 'XMLHttpRequest',
        token: '123456',
      },
      data: data,  //{}

      // `responseType` indicates the type of data that the server will respond with
      // options are: 'arraybuffer', 'document', 'json', 'text', 'stream'
      //   browser only: 'blob'
      responseType: 'json', // default
      responseEncoding: 'utf8', // default
      validateStatus: function (status) {
        return status >= 200 && status < 300; // default
      },
    }).then((res1) => {
      console.log("进入then-res1")
      resolve(res1.data);
    }).catch(err => {
      console.log("进入then-err")
      reject(err.data)
    });

  });



}


export function req_action_(url, method, data) {

  axios.defaults.headers.token = window.sessionStorage.getItem('token' || 123456);
  axios.defaults.headers.langs = window.sessionStorage.getItem('langs' || 'zh');
  //使用axios下面的create([config])方法创建axios实例，其中config参数为axios最基本的配置信息。
  return new Promise((resolve, reject) => {

    console.log("进入promise--boble");
    console.log("base_url", base_url);
    console.log(url, method, data)
    axios.create({
      baseUrl: base_url, //请求后端数据的基本地址，自定义
      timeout: 10000,                  //请求超时设置，单位ms
      url: url,
      method: method || 'get', // default
      headers: {
        'X-Requested-With': 'XMLHttpRequest',
        token: window.sessionStorage.getItem('token' || 123456),
      },
      data: data,  //{}

      // `responseType` indicates the type of data that the server will respond with
      // options are: 'arraybuffer', 'document', 'json', 'text', 'stream'
      //   browser only: 'blob'
      // responseType: 'json', // default
      responseType: "blob",
      responseEncoding: 'utf8', // default
      validateStatus: function (status) {
        console.log("status", status)
        return status >= 200 && status < 300; // default
      },
    })

  }).then((res1) => {
    console.log("进入then-res1")
    resolve(res1.data);
  }).catch(err => {
    console.log("进入then-err")
    reject(err.data)
  });



}


//导出我们建立的axios实例模块，ES6 export用法
